﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Inbound
{
    public partial class SelectSupplier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string sql = @"select docentry,cardcode,cardname,docdate from opor where docstatus='o'";

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string docentry = gridView.DataKeys[gridViewRow.RowIndex]["docentry"].ToString();
            string cardcode = gridView.DataKeys[gridViewRow.RowIndex]["cardcode"].ToString();
            string cardname = gridView.DataKeys[gridViewRow.RowIndex]["cardname"].ToString();

            switch (e.CommandName)
            {
                case "CmdSelect":

                    DataTable dtSupplier = GetSupplier(docentry);
                    string warehouse = GetWarehouse(docentry);
                    string supplierID = string.Empty;
                    string supplierName = string.Empty;

                    if (dtSupplier != null && dtSupplier.Rows.Count > 0)
                    {
                        supplierID = dtSupplier.Rows[0]["CardCode"].ToString();
                        supplierName = dtSupplier.Rows[0]["CardName"].ToString();
                    }

                    string returnValue = supplierID + "|" + supplierName + "|" + warehouse + "|" + docentry;
                    Response.Write("<script>window.returnValue='" + returnValue + "';window.opener = null;window.open('', '_self', '');window.close();</script>");
                    break;
                default:
                    break;
            }
        }

        private DataTable GetSupplier(string docentry)
        {
            string sql = @"SELECT top 1 t1.CardCode,t1.CardName
                            FROM OPOR t0
                            JOIN OCRD t1 ON t1.cardcode=t0.cardcode
                            WHERE t0.docentry=@docentry";

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@docentry", DbType.String, docentry);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
            return dataTable;
        }

        private string GetWarehouse(string docentry)
        {
            string sql = @"SELECT top 1 T1.whsname
                            FROM POR1 T0
                            JOIN OWHS T1 ON T1.whscode=T0.whscode
                            WHERE T0.docentry=@docentry";

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@docentry", DbType.String, docentry);
            object obj = database.ExecuteScalar(dbCommand);
            return obj != null ? obj.ToString() : string.Empty;
        }
    }
}